<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>创新创业中心管理平台</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <c:import url="admin_home.jsp"></c:import>
    <!-- 隐藏域 -->
    <input type="hidden" id="hidden_lastDate" value="${lastDate}">
    <input type="hidden" id="hidden_firstDate" value="${firstDate}">
    <!-- 主体内容 -->
    <div class="layui-body container" style="margin-top: 100px">
     		<div class="layui-row">
     		
     			<button id="add_btn" style="float: left;" type="button" class="btn btn-default layui-col-md-offset2">
     				<i class="layui-icon" style="color: #1E9FFF;">&#xe654</i>
     				开始录入
     			</button>
     			
     			<form class="layui-form  layui-col-md5" action="${pageContext.request.contextPath}/admin/stuInfo.do" method="post">
     				<div class="layui-form-item">
					   <div class="layui-input-inline layui-col-md-offset2 layui-col-md4">
					     <input type="text" id="stuName" name="stuName" lay-verify="required" placeholder="输入学生姓名" autocomplete="off" class="layui-input">
					   </div>
					   <div class="layui-col-md4">
					   	<button class="btn btn-primary" type="submit">查询</button>  
					   </div>
					    
					</div>
				</form>
				<div class="layui-col-md2">
     				
     			</div>
     			
     		</div>
     		
     		<!-- 显示表格数据 -->
     		<div class="layui-row">
     			<div class=" layui-col-md-offset2 layui-col-md8" >
     				<table class="table table-bordered">
     					<thead>
     							<h3 class="text-center">
     							创业中心查人记录表
     							
     							</h3>
     							
     						<tr>
     							<th colspan="3" class="text-center">日期</th>
     							<th colspan="3" class="text-center">
     								<input type="text" class="layui-input" id="check_date" style="width: 200px;margin: 0 auto;">
     							</th>
     							
     						</tr>
     						<tr>
     							<th colspan="3" class="text-center">清点时间</th>
     							
     							<td>9:00~10:00</td>
     							<td>16:00~17:00</td>
     							<td>19:00~20:00</td>
     						</tr>
     						<tr>
     							<th class="text-center">房间号</th>
     							<th class="text-center">项目名</th>
     							<th class="text-center">应到人数</th>
     							<th colspan="6"></th>
     						</tr>
     					</thead>
     					<tbody id="records">
     						<c:forEach items="${recordes}" var="record">
							<tr>
								<td>${record.value[0].roomId}</td>
								<td>${record.value[0].projectName}</td>
								<td>${record.value[0].memberNum}</td>
								<td>${record.value[0].member_num}</td>
								<td>${record.value[1].member_num}</td>
								<td>${record.value[2].member_num}</td>
							</tr>
						</c:forEach>
     						
     					</tbody>
     					
     				</table>
     			</div>
     		</div>
     		<div class="layui-row">
     			<div class="layui-col-md-offset9" id="save_div">
     				
     			</div>
     		</div>
     </div>
<script type="text/javascript">
//日期选择

layui.use('laydate', function(){
  var laydate = layui.laydate;
  
  //执行一个laydate实例
  laydate.render({
    elem: '#check_date' //指定元素
    ,value:$("#hidden_lastDate").val()
    ,min:$("#hidden_firstDate").val()
    ,max:getNowDay()
    ,done: function(value, date, endDate){
    	console.log(value);
    	selectRecordes(value);
    }
  });
});
//获取当前时间(yyyy-MM-dd)
function getNowDay(){
	var now_day=new Date();
	var day=now_day.getDate();
	var month=now_day.getMonth()+1;
	var year=now_day.getFullYear();
	var now_day_String=year+"-"+month+"-"+day;
	console.log("now_day_String:"+now_day_String);
	return now_day_String;
}
//选择日期查看数据
function selectRecordes(select_date){
	var lastDate=$("#hidden_lastDate").val();
	var d1=new Date(select_date.replace("-","/").replace("-","/"));
	var d2=new Date(lastDate.replace("-","/").replace("-","/"));
	if(select_date>lastDate){
		layer.msg(select_date+'日数据未录入.', {icon: 2});
	}else{
		$.ajax({
			url:"${pageContext.request.contextPath}/admin_stu/getRecordes.do",
			data:"date="+select_date,
			success:function(result){
				console.log(result);
				if(result.code==200){
					layer.msg('该数据获取失败！', {icon: 2});
				}else{
					$("#records").empty();
					var recordes=result.extend.recordes;
					$.each(recordes,function(index,row){
						var r=$("<tr></tr>");
						var roomId=$("<td></td>").append(row[0].roomId);
						var projectName=$("<td></td>").append(row[0].projectName);
						var memberNum=$("<td></td>").append(row[0].memberNum);
						var num_1=$("<td></td>").append(row[0].member_num);
						var num_2=$("<td></td>").append(row[1].member_num);
						var num_3=$("<td></td>").append(row[2].member_num);
						r.append(roomId).append(projectName).append(memberNum)
							.append(num_1).append(num_2).append(num_3).appendTo("#records");
					});
				}
			}
		});
	}
}
var projectIds;//录入数据的项目id
var roomIds;//录入数据的房间号
//保存录入的数据
function save_records(){
	var check_date=$("#check_date").val();
	if(check_date==null||check_date==""){
		layer.msg('请选择日期！', {icon: 2});
	}else{
		var lastDate=$("#hidden_lastDate").val();
		var d1=new Date(check_date.replace("-","/").replace("-","/"));
		var d2=new Date(lastDate.replace("-","/").replace("-","/"));
		console.log(d1<=d2);
		if(d1<=d2){
			layer.msg('该日的记录已录入.', {icon: 2});
			return;
		}else{
			//询问框
			layer.confirm('确定录入'+check_date+'日的数据吗？', {
			  btn: ['确定','检查数据'] //按钮
			}, 
			function(){
				var pros=new Array(projectIds.length);
				$.each(projectIds,function(index,id){
					//console.log(id);
					var num_1=$("#"+id+"_1 option:selected").val();
					var num_2=$("#"+id+"_2 option:selected").val();
					var num_3=$("#"+id+"_3 option:selected").val();;
					pros[index]={"array":[num_1,num_2,num_3],"projectId":id,"roomId":roomIds[index]};
					//console.log(pros[index]);
				});
				var data={"date":check_date,"pros":pros};
				console.log(data);
				
				$.ajax({
					url:"${pageContext.request.contextPath}/admin_stu/add_checkInfo.do",
					type:"POST",
					contentType:"application/json",
					data:JSON.stringify(data),
					dataType:"JSON",
					success:function(result){
						if(result.code==100){
							
							layer.msg('录入成功!', {icon: 1,time:2000,end:function(){
								location.reload();
							}});
						}else{
							layer.msg('录入失败!', {icon: 2});
						}
					}
				});
			}, 
			function(){
			  layer.close();
			  return;
			});
		}
		
		
	}
	
	$("#save_btn").blur();
}
//录入数据页面
$("#add_btn").click(function(){
	var check_date=$("#check_date").val();
	console.log(check_date);
	$.ajax({
		type:"GET",
		data:{"check_date":check_date},
		url:"${pageContext.request.contextPath}/admin_stu/click_addBtn.do",
		success:function(result){
			console.log(result);
			if(result.code==100){
				$("#records").empty();
				var projectInfo=result.extend.list;
				projectIds=new Array(projectInfo.length);
				roomIds=new Array(projectInfo.length);
		    	$.each(projectInfo,function(index,pro){
		    		projectIds[index]=pro.projectId;
		    		roomIds[index]=pro.roomId;
		    		var row=$("<tr></tr>");
					var select=$("<select></select>").addClass("form-control").attr("style","height: 30px");
					for(var i=0;i<=pro.memberNum;i++){
						select.append($("<option></option>").append(i));
					}
		    		row.append($("<td></td>").append(pro.roomId));
		    		row.append($("<td></td>").append(pro.projectName));
		    		row.append($("<td></td>").append(pro.memberNum));
		    		row.append($("<td></td>").append(select.attr("id",pro.projectId+"_1")));
		    		row.append($("<td></td>").append(select.clone().attr("id",pro.projectId+"_2")));
		    		row.append($("<td></td>").append(select.clone().attr("id",pro.projectId+"_3")));
		    		row.appendTo("#records");
		    	});
		    	$("#save_div").empty();
		    	var notSave_btn=$("<button></button>").attr("id","save_btn").addClass("btn btn-default")
					.attr("style","margin-right: 20px").attr("onclick","javascript:history.go(0)").append("取消");
 				var save_btn=$("<button></button>").attr("id","save_btn").addClass("btn btn-success")
 								.attr("onclick","save_records()").append("确认录入");
 				notSave_btn.appendTo("#save_div");
 				save_btn.appendTo("#save_div");
 				$("#add_btn").blur();
			}
			else{
				layer.msg('录入失败!', {icon: 2});
			}
		}
		
	});
});

</script>
  </body>
</html>